﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class D_Mix
    {
        public bool CreateOneMix(Mix_Info objMix_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Mix_Insert", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"MixID", objMix_Info.MixID));
            command.Parameters.Add(new SqlParameter(@"MixName", objMix_Info.MixName));
            command.Parameters.Add(new SqlParameter(@"Time", objMix_Info.Time));
            command.Parameters.Add(new SqlParameter(@"MixLocationID", objMix_Info.MixLocationID));
            command.Parameters.Add(new SqlParameter(@"Description", objMix_Info.Description));
            command.Parameters.Add(new SqlParameter(@"Quanlity", objMix_Info.Quanlity));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //**********************************************************************************       
        public bool EditOneMix(Mix_Info objMix_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Mix_Update", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@MixID", objMix_Info.MixID));
            command.Parameters.Add(new SqlParameter("@MixName", objMix_Info.MixName));
            command.Parameters.Add(new SqlParameter("@Time", objMix_Info.Time));
            command.Parameters.Add(new SqlParameter("@MixLocationID", objMix_Info.MixLocationID));
            command.Parameters.Add(new SqlParameter("@Description", objMix_Info.Description));
            command.Parameters.Add(new SqlParameter("@Quanlity", objMix_Info.Quanlity));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public bool RemoveOneMix(int MixID)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Mix_Delete", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"MixID", MixID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public DataTable GetAllMix()
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Mix_SelectAll", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public Mix_Info GetOneMix(int MixID)
        {
            Mix_Info objMix_Info = new Mix_Info();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Mix_SelectByID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"MixID", MixID));
            try
            {
                objConnection.Open();
                SqlDataReader ordMix = command.ExecuteReader(); //OjectRecordDatareader
                ordMix.Read();
                if (ordMix.HasRows)
                {
                    if (ordMix["MixID"] != DBNull.Value)
                    {
                        objMix_Info.MixID = Convert.ToInt16(ordMix["MixID"]);
                    }
                    if (ordMix["MixName"] != DBNull.Value)
                    {
                        objMix_Info.MixName = Convert.ToString(ordMix["MixName"]);
                    }
                    if (ordMix["Time"] != DBNull.Value)
                    {
                        objMix_Info.Time = Convert.ToDateTime(ordMix["Time"]);
                    }
                    if (ordMix["MixLocationID"] != DBNull.Value)
                    {
                        objMix_Info.MixLocationID = Convert.ToInt16(ordMix["MixLocationID"]);
                    }
                    if (ordMix["Description"] != DBNull.Value)
                    {
                        objMix_Info.Description = Convert.ToString(ordMix["Description"]);
                    }
                    if (ordMix["Quanlity"] != DBNull.Value)
                    {
                        objMix_Info.Quanlity = Convert.ToInt16(ordMix["Quanlity"]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return objMix_Info;
        }
    }
}
